package pers.zw.study.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Demo2 and demo3 together show the communication between two servlet using context
 */
@WebServlet(name = "ServletDemo2", urlPatterns = "/ServletDemo2")
public class ServletDemo2 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String data = "zw_Context";

        // ServletConfig对象中维护了ServletContext的引用
        ServletContext context = this.getServletConfig().getServletContext();
        context.setAttribute("data", data);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
